package cs236369.webserver.queue.managers;

import java.util.ArrayList;
import java.util.concurrent.ConcurrentLinkedQueue;

public abstract class TaskManager<T> {
	ConcurrentLinkedQueue<T> taskQueue = new ConcurrentLinkedQueue<T>();
	ArrayList<Thread> workerThreads = null;

	public TaskManager(int sizeOfSocketQueue) {
		workerThreads = new ArrayList<Thread>(sizeOfSocketQueue);
		for (int i = 0; i < sizeOfSocketQueue; ++i) {
			Thread worker = newWorker(taskQueue);
			workerThreads.add(worker);
			worker.start();
		}
	}

	public void enQueue(T taskObject) {
		synchronized (taskQueue) {
			taskQueue.offer(taskObject);
			taskQueue.notifyAll(); //so no tasks become forever stuck in the queue
		}
	}

	protected abstract Thread newWorker(ConcurrentLinkedQueue<T> taskQueue);

}